package com.sgb.kjwl.viewmodel.machine;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.AskLeaseLandEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseItemEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MLog;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AskLeaseLandModel3 extends ViewModel {


    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> listDataLiveData = new MutableLiveData<>();

    public MutableLiveData<String> msgLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> showLoad = new MutableLiveData<>();
    public MutableLiveData<AskLeaseLandEntity> askLeaseLandEntityLiveData = new MutableLiveData<>();
    public MutableLiveData<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>> askLeaseLandEntityListLiveData = new MutableLiveData<>();
    public MutableLiveData<List<DictEntity3>> entity3LiveData = new MutableLiveData<>();//获取付款方式
    public MutableLiveData<List<DictEntity3>> shiLiveData = new MutableLiveData<>(); //供水供电方式
    public MutableLiveData<List<DictEntity3>> jixieLiveData = new MutableLiveData<>();//机械作业方式
    public MutableLiveData<List<DictEntity3>> rizhaoLiveData = new MutableLiveData<>();//  日照情况
    public MutableLiveData<String> publishLiveData = new MutableLiveData<>();// 是否发布成功
    public MutableLiveData<Integer> selectFragLiveData = new MutableLiveData<>();

    public AskLeaseLandEntity entity = new AskLeaseLandEntity();//发布数据

    //获取省市区
    public void getAddressModel() {
        showLoad.postValue(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                showLoad.postValue(false);
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    listDataLiveData.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                showLoad.postValue(false);
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }





    //获取详情
    public void getDetail(String no) {
        showLoad.postValue(true);
        APIManager.getInstance().getMachineAPI().getDetailLand(no).enqueue(new Callback<BaseEntity<AskLeaseLandEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<AskLeaseLandEntity>> call, Response<BaseEntity<AskLeaseLandEntity>> response) {
                showLoad.postValue(false);
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseLandEntityLiveData.postValue(response.body().getData());
                } else {
                    msgLiveData.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<AskLeaseLandEntity>> call, Throwable t) {
                showLoad.postValue(false);
            }
        });
    }

    //  发布求租信息
    public void addOrUpdate(AskLeaseLandEntity askLeaseLandEntity) {
        showLoad.postValue(true);
        APIManager.getInstance().getMachineAPI().addOrUpdate(askLeaseLandEntity).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                showLoad.postValue(false);
                if (response.body() == null || response.body().getState() == null) {
                    msgLiveData.postValue("发布失败");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData() instanceof String) {
                        publishLiveData.postValue((String) response.body().getData());
                    }else {
                        msgLiveData.postValue("发布失败");
                    }
                } else {
                    msgLiveData.postValue("发布失败");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                msgLiveData.postValue("发布失败...");
                showLoad.postValue(false);
            }
        });
    }

    //获取类型分类数据
    public void getTypeModel() {
        APIManager.getInstance().getLeaseAPI().getAllCategoryLand().enqueue(new Callback<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>> call, Response<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData() && null != response.body().getData()) {
                        askLeaseLandEntityListLiveData.postValue(response.body().getData());
                    } else {
                        msgLiveData.postValue("暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>> call, Throwable t) {
                Log.v("dd", "sdsdd");
            }
        });
    }

    // 获取付款方式
    public void getAllPayWay() {
        APIManager.getInstance().getMachineAPI().getAllPayWay().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        entity3LiveData.postValue(response.body().getData());
                    } else {
                        msgLiveData.postValue("暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    // 获取供电方式
    public void getPowerWay() {
        APIManager.getInstance().getMachineAPI().getPowerWay().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        shiLiveData.postValue(response.body().getData());
                    } else {
                        msgLiveData.postValue("暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    // 获取机械作业方式
    public void getMechanicalWork() {
        APIManager.getInstance().getMachineAPI().getMechanicalWork().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        jixieLiveData.postValue(response.body().getData());
                    } else {
                        msgLiveData.postValue("暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    // 日照情况
    public void getSunshineSupply() {
        APIManager.getInstance().getMachineAPI().getSunshineSupply().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        rizhaoLiveData.postValue(response.body().getData());
                    } else {
                        msgLiveData.postValue("暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }


}
